﻿using System;
using System.Collections.Generic;
using System.Linq;
using DM4;
using sscore;

namespace DM4Client
{
    public partial class FrmMain
    {
        #region users
        public static XMLInfo GetUserList()
        {
            return SetUsrSessionId(Dm4Srv.GetUserList(usrSessionId));
        }
        #endregion users

        //-----------Groups------------------------------
        public static XMLInfo GetGroupList(bool showall = false) 
        {
            return SetUsrSessionId(Dm4Srv.GetGroupList(usrSessionId, showall));
        }

        public static OperationResult AddGroup(bool publicGroup, string name, string description, string homedir, int scriptId)
        {
            return SetUsrSessionId(Dm4Srv.AddGroup(usrSessionId, publicGroup, name, description, homedir, scriptId));
        }

        public static OperationResult EditGroup(int grpId, string name, string description, string homedir, int scriptId)
        {
            return SetUsrSessionId(Dm4Srv.EditGroup(usrSessionId, grpId, name, description, homedir, scriptId));
        }


        public static OperationResult DeleteGroup(int grpId)
        {
            return SetUsrSessionId(Dm4Srv.DeleteGroup(usrSessionId, grpId));
        }

        //------------Scripts-----------------------------
        public static XMLInfo GetScriptList()
        {
            return SetUsrSessionId(Dm4Srv.GetScriptList(usrSessionId));
        }

        public static OperationResult AddScript(string name, string description, string text)
        {
            return SetUsrSessionId(Dm4Srv.AddScript(usrSessionId, name, description, text));
        }

        public static OperationResult EditScript(int scriptId, string name, string description, string text)
        {
            return SetUsrSessionId(Dm4Srv.EditScript(usrSessionId, scriptId, name, description, text));
        }

        public static OperationResult DeleteScript(int scriptId)
        {
            return SetUsrSessionId(Dm4Srv.DeleteScript(usrSessionId, scriptId));
        }

        public static OperationResult RunScript(int scrId = DM4Consts.DefaultNULLId, string rshSessionId = null, string modCode = null, string modParams = null)
        {
            return SetUsrSessionId(Dm4Srv.RunScript(usrSessionId, scrId, rshSessionId, new string[]{modCode}, modParams));
        }

        public static OperationResult RunScript(string[] modCode, string modParams = null)
        {
            return SetUsrSessionId(Dm4Srv.RunScript(usrSessionId, DM4Consts.DefaultNULLId, null, modCode, modParams));
        }

        ////-------------MailSendTemplates----------------------------
        public static XMLInfo GetMailSendTemplateList()
        {
            return SetUsrSessionId(Dm4Srv.GetMailSendTemplateList(usrSessionId));
        }

        public static OperationResult AddMailSendTemplate(string name, string subject, string body)
        {
            return SetUsrSessionId(Dm4Srv.AddMailSendTemplate(usrSessionId, name, subject, body));
        }

        public static OperationResult EditMailSendTemplate(int templateId, string name, string subject, string body)
        {
            return SetUsrSessionId(Dm4Srv.EditMailSendTemplate(usrSessionId, templateId, name, subject, body));
        }

        public static OperationResult DeleteMailSendTemplate(int templateId)
        {
            return SetUsrSessionId(Dm4Srv.DeleteMailSendTemplate(usrSessionId, templateId));
        }

        ////-------------MailAccounts----------------------------
        public static XMLInfo GetMailAccountList()
        {
            return SetUsrSessionId(Dm4Srv.GetMailAccountList(usrSessionId));
        }

        public static OperationResult AddMailAccount(string mail, string sendServer, string sendPort, string sendUser, string sendPassword, bool sendUseSsl
            , string receiveServer, string receivePort, string receiveUser, string receivePassword, bool receiveUseSsl)
        {
            return SetUsrSessionId(Dm4Srv.AddMailAccount(usrSessionId, mail, sendServer, sendPort, sendUser, sendPassword, sendUseSsl
                                                        , receiveServer, receivePort, receiveUser, receivePassword, receiveUseSsl));
        }

        public static OperationResult EditMailAccount(int accountId, string mail, string sendServer, string sendPort, string sendUser, string sendPassword, bool sendUseSsl
            , string receiveServer, string receivePort, string receiveUser, string receivePassword, bool receiveUseSsl)
        {
            return SetUsrSessionId(Dm4Srv.EditMailAccount(usrSessionId, accountId, mail, sendServer, sendPort, sendUser, sendPassword, sendUseSsl
                                                        , receiveServer, receivePort, receiveUser, receivePassword, receiveUseSsl));
        }

        public static OperationResult DeleteMailAccount(int accountId)
        {
            return SetUsrSessionId(Dm4Srv.DeleteMailAccount(usrSessionId, accountId));
        }
        //-----------Mail Listener-------------------------------------------------------------------------
        public static XMLInfo GetMailSendersList()
        {
            return SetUsrSessionId(Dm4Srv.GetMailSendersList(usrSessionId));
        }

        public static XMLInfo GetLettersList(string expression = "")
        {
            return SetUsrSessionId(Dm4Srv.GetLettersList(usrSessionId, expression));
        }

        public static XMLInfo GetMailRulesList(string expression = "")
        {
            return SetUsrSessionId(Dm4Srv.GetMailRulesList(usrSessionId, expression));
        }

        public static OperationResult AddMailSender(string mail)
        {
            return SetUsrSessionId(Dm4Srv.AddMailSender(usrSessionId, mail));
        }

        public static OperationResult EditMailSender(int id, string mail)
        {
            return SetUsrSessionId(Dm4Srv.EditMailSender(usrSessionId, id, mail));
        }

        public static OperationResult DeleteMailSender(int id)
        {
            return SetUsrSessionId(Dm4Srv.DeleteMailSender(usrSessionId, id));
        }


        public static OperationResult AddLetter(int senderId, string description)
        {
            return SetUsrSessionId(Dm4Srv.AddLetter(usrSessionId, senderId, description));
        }

        public static OperationResult EditLetter(int id, int senderId, string description)
        {
            return SetUsrSessionId(Dm4Srv.EditLetter(usrSessionId, id, senderId, description));
        }

        public static OperationResult DeleteLetter(int id)
        {
            return SetUsrSessionId(Dm4Srv.DeleteLetter(usrSessionId, id));
        }
        public static OperationResult AddMailRule(string ruleName, string rule, MailRuleType mrt)
        {
            return SetUsrSessionId(Dm4Srv.AddMailRule(usrSessionId, ruleName, rule, mrt));
        }

        public static OperationResult EditMailRule(int id, string ruleName, string rule, MailRuleType mrt)
        {
            return SetUsrSessionId(Dm4Srv.EditMailRule(usrSessionId, id, ruleName, rule, mrt));
        }

        public static OperationResult DeleteMailRule(int id)
        {
            return SetUsrSessionId(Dm4Srv.DeleteMailRule(usrSessionId, id));
        }

        public static XMLInfo GetMailLettersRulesList(int letterId)
        {
            return SetUsrSessionId(Dm4Srv.GetMailLettersRulesList(usrSessionId, letterId));
        }

        public static OperationResult AddMailLettersRule(int letterId, int ruleId)
        {
            return SetUsrSessionId(Dm4Srv.AddMailLettersRule(usrSessionId, letterId, ruleId));
        }

        public static OperationResult DeleteMailLettersRule(int letterId, int ruleId)
        {
            return SetUsrSessionId(Dm4Srv.DeleteMailLettersRule(usrSessionId, letterId, ruleId));
        }
        #region Daemon function
        public static OperationResult ExecuteDaemonCommand(string code, string command, object parameters)
        {
            return SetUsrSessionId(Dm4Core.ExecuteDaemonCommand(usrSessionId, code, command, parameters));
        }

        public static OperationResult StartDaemon(string code = "", bool startDaemonInDebug = true)
        {
            return SetUsrSessionId(Dm4Core.StartDaemon(usrSessionId, code, startDaemonInDebug));
        }

        public static OperationResult StopDaemon(string code = "")
        {
            return SetUsrSessionId(Dm4Core.StopDaemon(usrSessionId, code));
        }

        public static OperationResult RestartDaemon(string code = "")
        {
            return SetUsrSessionId(Dm4Core.RestartDaemon(usrSessionId, code));
        }

        private static OperationResult GetDaemonsList()
        {
            return SetUsrSessionId(Dm4Core.GetDaemonsList(usrSessionId));
        }
        #endregion Daemon function
        
        //work
        public static XMLInfo GetWorkTree(bool show_all = false)
        {
            return SetUsrSessionId(Dm4Srv.GetWorkTree(usrSessionId, show_all));
        }

        #region Session function
        public static XMLInfo GetSessionsList()
        {
            return SetUsrSessionId(Dm4Srv.GetSessionsList(usrSessionId));
        }

        public static OperationResult AbortSession(string sessionId)
        {
            return SetUsrSessionId(Dm4Srv.AbortSession(usrSessionId, sessionId));
        }

        public static OperationResult ResumeSession(string sessionId)
        {
            return SetUsrSessionId(Dm4Srv.ResumeSession(usrSessionId, sessionId));
        }

        public static OperationResult AbortTask(string taskId)
        {
            return SetUsrSessionId(Dm4Srv.AbortTask(usrSessionId, taskId));
        }

        public static OperationResult ResumeTask(string taskId)
        {
            return SetUsrSessionId(Dm4Srv.ResumeTask(usrSessionId, taskId));
        }

        public static OperationResult AbortScript(string ScriptId)
        {
            return SetUsrSessionId(Dm4Srv.AbortScript(usrSessionId, ScriptId));
        }

        public static OperationResult ResumeScript(string ScriptId)
        {
            return SetUsrSessionId(Dm4Srv.ResumeScript(usrSessionId, ScriptId));
        }
        #endregion
    }
}
